#include"../../Source/Detail.hpp"

#define HOST "127.0.0.1"
#define USER "root"
#define PASS "2794721791pfg."
#define PORT 3306
#define DBNAME "test"

void test_mysql()
{
    MYSQL* mysql=Detail::MySql::MySql_Create(HOST,USER,PASS,DBNAME);
    const char* sql="insert stu values(null,39,'李四',10,20,30);";
    bool ret=Detail::MySql::MySql_Exec(mysql,sql);
    if(!ret)
    {
        std::cout<<"mysql failed"<<std::endl;
    }

    Detail::MySql::MySql_Destroy(mysql);

}
void test_json()
{
    Json::Value stu;
    stu["姓名"] = "张三";
    stu["年龄"] = 19;
    stu["成绩"].append(77.5);
    stu["成绩"].append(88);
    stu["成绩"].append(99.5);
    Json::Value fav;
    fav["书籍"] = "西游记";
    fav["运动"] = "游泳";
    stu["爱好"] = fav;

    std::string ser;
    if(Detail::JSON::Serialize(stu,ser))
    {
        std::cout<<ser<<std::endl;
    }
    if(!stu["书籍"].isNull())
    {
        std::cout<<"爱好：书籍："<<stu["爱好"]["书籍"].asString()<<std::endl;
    }

    Json::Value unser;
    std::string str2=R"({"姓名":"李四","年龄":20,"成绩":[28.9,19.6,35]})"; //R"(xxx...)"：不对xxx...字符串做任何转义，是什么样，输出还是什么样
    if(Detail::JSON::UnSerialize(str2,unser))
    {
        std::cout<<"姓名："<<unser["姓名"].asString()<<std::endl;
        std::cout<<"年龄："<<unser["年龄"].asInt()<<std::endl;
        int sz=unser["成绩"].size();
        std::cout<<"成绩：[";
        int i=0;
        for(i=0;i<sz-1;i++)
        {
            std::cout<<unser["成绩"][i].asFloat()<<",";
        }
        std::cout<<unser["成绩"][i].asFloat()<<"]"<<std::endl;
    }
}
void test_split()
{
    std::string str="hello,haha,,heihei,,,,,nihao,,,,zaijian,,,,";
    std::vector<std::string> res;
    int ret=Detail::StringSplit::Split(str,",",res);
    for(auto e:res)
    {
        std::cout<<e<<std::endl;
    }
}

void test_read()
{   
    std::string file="./Makefile";
    std::string res;
    bool ret=Detail::ReadFile::Read(file,res);
    std::cout<<res<<std::endl;
}

int main()
{
    test_mysql();
    test_json();
    test_split();
    test_read();
    return 0;
}